<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class ResultsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveResults($Saveresults)
    {
		$data = array(
			'user_id' 	  			=> $Saveresults['user_id'],
			'course_id'				=> $Saveresults['course_id'],
			'subject_id'			=> $Saveresults['subject_id'],
			'question_id' 	  		=> $Saveresults['question_id'], 	
			'user_answer'			=> $Saveresults['user_answer'],
			'status' 				=> 1, 				
		);
		
		$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }
	public function saveQuestion($question_id,$totalresults)
    {
		$data = array(
			'user_id' 	  			=> $totalresults['user_id'],
			'course_id'				=> $totalresults['course_id'],
			'subject_id'			=> $totalresults['subject_id'],
			'question_id' 	  		=> $question_id, 	
			'user_answer'			=> $totalresults['user_answer'],
			'status' 				=> 1, 				
		);
		
		$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }
	
	public function getResults($totalresult){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_answer', new Expression('pedia_answer.answer_id=pedia_results.answer_id'),array('correct_answer'),'left');
	    $select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_results.course_id'),array('course_name'),'left');
		$select->join('pedia_questions', new Expression('pedia_questions.question_id=pedia_results.question_id'),array('question_name','level'),'left');
		$select->where('pedia_answer.correct_answer=pedia_results.user_answer');
		$select->where('pedia_results.user_id="'.$user_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
	
	public function getQuestions($question_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_questions', new Expression('pedia_questions.question_id=pedia_results.question_id'),array('*'),'left');
		$select->where('pedia_results.question_id="'.$question_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
	//Get Questions In results tableGateway
	public function getQuestionData($q_data){
		$select = $this->tableGateway->getSql()->select();
		$select->where('pedia_results.question_id="'.$q_data['que_id'].'"');
		$select->where('pedia_results.subject_id="'.$q_data['sub_id'].'"');
		$select->where('pedia_results.course_id="'.$q_data['cou_id'].'"');
		$select->where('pedia_results.status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
	public function updateResults($Upresults)
    {
		
		$data = array(
			'user_id' 	  			=> $Upresults['user_id'],
			'course_id'				=> $Upresults['course_id'],
			'subject_id'			=> $Upresults['subject_id'],
			'question_id' 	  		=> $Upresults['question_id'], 	
			'user_answer'			=> $Upresults['user_answer'],
			'status' 				=> 1, 				
		);
		
		$updatedData=$this->tableGateway->update($data, array('question_id' => $Upresults['question_id'],'status'=> '1'));
		return 	$updatedData;		
    }
	
	public function updateResultsStatus($results)
    {
		$data = array(
			'status' 				=> 0, 				
		);
		
		$updaed_status=$this->tableGateway->update($data, array('user_id' => $results['user_id']));
		return 	$updaed_status;		
    }
	//Update Questions
	public function updateQuestion($q_id,$data)
    {
		$data = array(
			'question_id' 				=> $q_id, 				
		);
		
		$updaed_status=$this->tableGateway->update($data, array('question_id' => $q_id,'status'=>'1'));
		return 	$updaed_status;		
    }
	public function getResultData($reults_data){
	
		$select = $this->tableGateway->getSql()->select();
		$select->where('question_id="'.$reults_data['question_id'].'"');
		$select->where('status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
	
	public function checkQuestion($q_id){
	
		$select = $this->tableGateway->getSql()->select();
		$select->where('question_id="'.$q_id.'"');
		$select->where('status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	}
	
	public function getTotalResults($question_id,$totalresult){
	
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_answer', new Expression('pedia_answer.question_id=pedia_results.question_id'),array('*'),'left');
		$select->where('pedia_answer.correct_answer=pedia_results.user_answer');
		$select->where('pedia_results.user_id="'.$totalresult['user_id'].'"');
		$select->where('pedia_results.subject_id="'.$totalresult['subject_id'].'"');
		$select->where('pedia_answer.level="'.$totalresult['level'].'"');
		$select->where('pedia_results.status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
		
	}
	//checkdata
	
	public function getcheckData($q_data){
		$select = $this->tableGateway->getSql()->select();
		$select->where('pedia_results.question_id="'.$q_data['question_id'].'"');
		$select->where('pedia_results.subject_id="'.$q_data['subject_id'].'"');
		$select->where('pedia_results.course_id="'.$q_data['course_id'].'"');
		$select->where('pedia_results.user_id="'.$q_data['user_id'].'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	} 
	public function checkResultData($questionrandom_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('pedia_results.question_id="'.$questionrandom_id.'"');
		$select->where('pedia_results.status = 1 ');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	
	} 
	
	//Results tableGateway
	
	public function getResultsQuestionslist($q_id,$sub_id,$cou_id,$user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_questions', new Expression('pedia_results.question_id=pedia_questions.question_id'),array('*'),'left');
		$select->where('pedia_results.course_id="'.$cou_id.'"');
		$select->where('pedia_results.subject_id="'.$sub_id.'"');
		$select->where('pedia_results.question_id="'.$q_id.'"');
		$select->where('pedia_results.user_id="'.$user_id.'"');
		$select->where('pedia_results.status=1');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	//Get Question Results (recent)
	
	public function getQuestionResultsData($courseId,$user_id,$level,$subject_id,$question_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_questions', new Expression('pedia_questions.question_id=pedia_results.question_id'),array('level'),'left');
		$select->where('pedia_results.course_id="'.$courseId.'"');
		$select->where('pedia_results.subject_id="'.$subject_id.'"');
		$select->where('pedia_results.user_id="'.$user_id.'"');
		$select->where('pedia_questions.level="'.$level.'"');
		$select->where('pedia_results.question_id="'.$question_id.'"');
		$select->where('pedia_results.status=1');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
}